package com.shire.shire_interface.service.invite;

import com.shire.shire_interface.dao.invite.WxInviteSubTaskDao;
import com.shire.shire_interface.dao.invite.WxInviteTaskDao;
import com.shire.shire_interface.model.friend.WxFriendLog;
import com.shire.shire_interface.model.invite.WxInviteSubTask;
import com.shire.shire_interface.model.invite.WxInviteTask;
import com.shire.shire_interface.model.invite.WxInviteTaskExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者:  sll
 * 时间:  2019/8/9 15:30
 */
@Service
public class WxInviteTaskService {

    @Resource
    private WxInviteTaskDao wxInviteTaskDao;
    @Resource
    private WxInviteSubTaskDao wxInviteSubTaskDao;

    public Integer inviteFriend(WxInviteTask wxInviteTask){

        return wxInviteTaskDao.insertSelective(wxInviteTask);
    }

    public List<WxInviteTask> getInviteCsHistory(WxInviteTaskExample example) {
        return wxInviteTaskDao.selectByExample(example);
    }

    public List<WxInviteTask> selectByExample(WxInviteTaskExample wxInviteTaskExample) {
         return wxInviteTaskDao.selectByExample(wxInviteTaskExample);
    }

    public WxInviteTask selectByPrimaryKey(Integer taskId) {
        return wxInviteTaskDao.selectByPrimaryKey(taskId);
    }

    public Integer updateByPrimaryKey(WxInviteTask wxInviteTask) {
        return wxInviteTaskDao.updateByPrimaryKey(wxInviteTask);
    }

    public Integer updateBySelective(WxInviteTask wxInviteTask) {

        return wxInviteTaskDao.updateByPrimaryKeySelective(wxInviteTask);
    }
    public int updateForResult(Integer subTaskId, WxFriendLog friendLog){
        WxInviteSubTask subTask = wxInviteSubTaskDao.selectByPrimaryKey(subTaskId);
        WxInviteTask task = wxInviteTaskDao.selectByPrimaryKey(subTask.getTaskid());
        task.setAddcount(task.getAddcount() + 1);

        wxInviteTaskDao.updateByPrimaryKey(task);

        if (friendLog.getAddresult() == 1) {
            subTask.setFriendwxid(friendLog.getFriendwxid());
            subTask.setAddstatus(2);
        }
        subTask.setFriendavatar(friendLog.getFriendavatar());
        subTask.setFriendnick(friendLog.getFriendnick());

        return wxInviteSubTaskDao.updateByPrimaryKeySelective(subTask);
    }
}
